package com.hxy.service.Impl;

import com.hxy.common.DateUtil;
import com.hxy.common.JsonResult;
import com.hxy.dao.PaydetailMapper;
import com.hxy.dao.UserInfMapper;
import com.hxy.dao.UserpopeMapper;
import com.hxy.pojo.Paydetail;
import com.hxy.pojo.UserInf;
import com.hxy.pojo.Userpope;
import com.hxy.pojo.vo.PaydetailVo;
import com.hxy.service.UserInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserInfServiceImpl implements UserInfService {
    @Autowired
    private UserInfMapper userInfoMapper;
    @Autowired
    private UserpopeMapper userpopeMapper;
    @Autowired
    private PaydetailMapper paydetailMapper;

    @Override
    public UserInf findByUsername(String username) {
        return userInfoMapper.findByUsername(username);
    }

    @Override
    public List<String> findPermissionNamesByUsername(String username) {
        System.out.println(username + "findPermissionNamesByUsername---------------");
        //根据username获取userinfo对象
        UserInf userinfo = userInfoMapper.findByUsername(username);
        System.out.println(userinfo.getStatusId() + "'''''''''''''''''''''''");
        //根据权限码查找权限名字集合
        List<String> permissionNames = userpopeMapper.getPermissionNames(userinfo.getStatusId());
        //对于根用户权限单独处理
        if (userinfo.getStatusId() == 3) {
            List<Userpope> permissions = userpopeMapper.selectAll();
            for (Userpope permission : permissions) {
                permissionNames.add(permission.getPopename());
            }
        }
        return permissionNames;
    }

    @Override
    public Integer findEidByUsername(String username) {
        return userInfoMapper.findEidByUsername(username);
    }

    @Override
    public JsonResult findOnePaydetailsByEid(Integer eidByUsername) {

        try {
            List<PaydetailVo> paydetailList = paydetailMapper.selectOnePaydetailByEid(eidByUsername);
            //倒序
            Collections.sort(paydetailList, new Comparator<PaydetailVo>() {

                /** 返回负数表示：o1 小于o2，
                 * 返回0 表示：o1和o2相等，
                 * 返回正数表示：o1大于o2。*/
                @Override
                public int compare(PaydetailVo o1, PaydetailVo o2) {
                    Long t1=DateUtil.StringDateToMills(o1.getCreateTime());
                    Long t2=DateUtil.StringDateToMills(o2.getCreateTime());
                    if (t1 < t2) {
                        return 1;
                    }
                    if (t1 == t2){
                        return 0;
                    }
                    return -1;
                }
            });
            return new JsonResult(0, "success", paydetailList);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(1, "fail", null);
        }

    }

    @Override
    public String findPermissionNameByUsername(String name) {
        String permission = userInfoMapper.findPermission(name);
        return permission;
    }
}
